﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using HYElectricKernel;

namespace YArchitech.Plumbing
{
	public class RectLayPoint
	{
		public List<List<XYZ>> PointSet
		{
			get
			{
				return this.pointSet;
			}
		}

		public List<XYZ> Points
		{
			get
			{
				return this.points;
			}
		}

		public XYZ PointEnd
		{
			get
			{
				return this.point2;
			}
		}

		public RectLayPoint(RectLayParamers paras)
		{
			this.rectLayParamers = paras;
			if (this.rectLayParamers.LessDistance > 0.0 || this.rectLayParamers.BeiShu > 0)
			{
				this.Calc(1);
				return;
			}
			this.Calc();
		}

		public RectLayPoint(RectLayParamers paras, bool blRectLay)
		{
			this.rectLayParamers = paras;
			if (this.rectLayParamers.LessDistance > 0.0 || this.rectLayParamers.BeiShu > 0)
			{
				this.Calc(1, blRectLay);
				return;
			}
			this.Calc();
		}

		private void Calc()
		{
			if (this.rectLayParamers.LayType == 0)
			{
				this.rectNum = new RectLayoutNum(this.rectLayParamers);
				this.pointSet = this.rectNum.CalcInsertPoints();
				return;
			}
			this.rectDis = new RectLayoutDis(this.rectLayParamers);
			this.pointSet = this.rectDis.CalcInsertPoints();
		}

		private void Calc(int N)
		{
			double num = Func.AngleToRadian(this.rectLayParamers.AngleRow);
			XYZ xyz = Geometry.RotateTo(this.rectLayParamers.PointDiagonal, this.rectLayParamers.PointBase, XYZ.BasisZ, -num);
			this.rectLayParamers.XDiagonalPoint = new XYZ(xyz.X, this.rectLayParamers.PointBase.Y, xyz.Z);
			this.rectLayParamers.YDiagonalPoint = new XYZ(this.rectLayParamers.PointBase.X, xyz.Y, xyz.Z);
			this.point2 = Geometry.RotateTo(this.rectLayParamers.XDiagonalPoint, this.rectLayParamers.PointBase, XYZ.BasisZ, num);
			if (this.point2.IsAlmostEqualTo(this.rectLayParamers.PointBase))
			{
				this.points = null;
				return;
			}
			ConnectHelper connectHelper = new ConnectHelper();
			if (this.rectLayParamers.LayType == 0)
			{
				this.points = connectHelper.CalculateLayFamilyInstanceXYZ(this.rectLayParamers.PointBase, this.point2, this.rectLayParamers.PointDiagonal, this.rectLayParamers.RowCount, this.rectLayParamers.ColCount, this.rectLayParamers.Margin, 0.0);
				return;
			}
			this.points = connectHelper.CalculateLaySprinklersXYZ(this.rectLayParamers.PointBase, this.point2, this.rectLayParamers.PointDiagonal, this.rectLayParamers.XSpace, this.rectLayParamers.YSpace, this.rectLayParamers.XMargin, this.rectLayParamers.YMargin, this.rectLayParamers.LessDistance, 0.0, this.rectLayParamers.BeiShu, false);
		}

		private void Calc(int N, bool blRectLay = true)
		{
			double num = Func.AngleToRadian(this.rectLayParamers.AngleRow);
			XYZ xyz = Geometry.RotateTo(this.rectLayParamers.PointDiagonal, this.rectLayParamers.PointBase, XYZ.BasisZ, -num);
			this.rectLayParamers.XDiagonalPoint = new XYZ(xyz.X, this.rectLayParamers.PointBase.Y, xyz.Z);
			this.rectLayParamers.YDiagonalPoint = new XYZ(this.rectLayParamers.PointBase.X, xyz.Y, xyz.Z);
			this.point2 = Geometry.RotateTo(this.rectLayParamers.XDiagonalPoint, this.rectLayParamers.PointBase, XYZ.BasisZ, num);
			ConnectHelper connectHelper = new ConnectHelper();
			if (this.rectLayParamers.LayType == 0)
			{
				this.points = connectHelper.CalculateLayFamilyInstanceXYZ(this.rectLayParamers.PointBase, this.point2, this.rectLayParamers.PointDiagonal, this.rectLayParamers.RowCount, this.rectLayParamers.ColCount, this.rectLayParamers.Margin, 0.0);
				return;
			}
			double num2 = Math.Abs(Math.Round(this.rectLayParamers.PointDiagonal.X - this.rectLayParamers.PointBase.X, 2));
			double num3 = Math.Abs(Math.Round(this.rectLayParamers.PointDiagonal.Y - this.rectLayParamers.PointBase.Y, 2));
			if (Math.Round(num3, 2) < 1.0)
			{
				if (num2 * 304.8 <= 2.0 * this.rectLayParamers.YMargin)
				{
					this.points = new List<XYZ>();
					this.points.Add(new XYZ((this.rectLayParamers.PointDiagonal.X + this.rectLayParamers.PointBase.X) / 2.0, this.rectLayParamers.PointBase.Y, 0.0));
					return;
				}
				this.points = connectHelper.CalculateLaySprinklersXYZ(this.rectLayParamers.PointBase, this.point2, this.rectLayParamers.PointDiagonal, this.rectLayParamers.XSpace, this.rectLayParamers.YSpace, this.rectLayParamers.XMargin, this.rectLayParamers.YMargin, this.rectLayParamers.LessDistance, 0.0, this.rectLayParamers.BeiShu, true);
				return;
			}
			else
			{
				if (Math.Round(num2, 2) >= 1.0)
				{
					if (num2 * 304.8 <= 2.0 * this.rectLayParamers.YMargin && num3 * 304.8 <= 2.0 * this.rectLayParamers.XMargin)
					{
						if (Math.Abs(this.rectLayParamers.PointDiagonal.X - this.rectLayParamers.PointBase.X) > this.accuracy && Math.Abs(this.rectLayParamers.PointDiagonal.Y - this.rectLayParamers.PointBase.Y) > this.accuracy)
						{
							this.points = new List<XYZ>();
							this.points.Add(new XYZ((this.rectLayParamers.PointDiagonal.X + this.rectLayParamers.PointBase.X) / 2.0, (this.rectLayParamers.PointDiagonal.Y + this.rectLayParamers.PointBase.Y) / 2.0, 0.0));
							return;
						}
					}
					else
					{
						if (num2 * 304.8 <= 2.0 * this.rectLayParamers.YMargin && num3 * 304.8 > 2.0 * this.rectLayParamers.XMargin)
						{
							this.points = connectHelper.CalculateWidthLessLaySprinklers(this.rectLayParamers.PointBase, this.point2, this.rectLayParamers.PointDiagonal, this.rectLayParamers.XSpace, this.rectLayParamers.YSpace, this.rectLayParamers.XMargin, this.rectLayParamers.YMargin, this.rectLayParamers.LessDistance, 0.0, this.rectLayParamers.BeiShu, 0);
							return;
						}
						if (num3 * 304.8 <= 2.0 * this.rectLayParamers.XMargin && num2 * 304.8 > 2.0 * this.rectLayParamers.YMargin)
						{
							this.points = connectHelper.CalculateWidthLessLaySprinklers(this.rectLayParamers.PointBase, this.point2, this.rectLayParamers.PointDiagonal, this.rectLayParamers.XSpace, this.rectLayParamers.YSpace, this.rectLayParamers.XMargin, this.rectLayParamers.YMargin, this.rectLayParamers.LessDistance, 0.0, this.rectLayParamers.BeiShu, 1);
							return;
						}
						if (num2 <= this.accuracy && this.rectLayParamers.PointBase.Y == this.point2.Y)
						{
							this.point2 = this.rectLayParamers.PointDiagonal;
						}
						this.points = connectHelper.CalculateLaySprinklersXYZ(this.rectLayParamers.PointBase, this.point2, this.rectLayParamers.PointDiagonal, this.rectLayParamers.XSpace, this.rectLayParamers.YSpace, this.rectLayParamers.XMargin, this.rectLayParamers.YMargin, this.rectLayParamers.LessDistance, 0.0, this.rectLayParamers.BeiShu, true);
					}
					return;
				}
				if (num3 * 304.8 <= 2.0 * this.rectLayParamers.XMargin)
				{
					this.points = new List<XYZ>();
					this.points.Add(new XYZ(this.rectLayParamers.PointBase.X, (this.rectLayParamers.PointDiagonal.Y + this.rectLayParamers.PointBase.Y) / 2.0, 0.0));
					return;
				}
				this.points = connectHelper.CalculateVerticalLaySprinklers(this.rectLayParamers.PointBase, this.rectLayParamers.PointDiagonal, this.rectLayParamers.PointDiagonal, this.rectLayParamers.XSpace, this.rectLayParamers.XMargin, this.rectLayParamers.LessDistance, 0.0, this.rectLayParamers.BeiShu);
				return;
			}
		}

		private RectLayParamers rectLayParamers;

		private RectLayoutDis rectDis;

		private RectLayoutNum rectNum;

		private List<List<XYZ>> pointSet;

		private List<XYZ> points;

		private XYZ point2;

		private readonly double accuracy = 0.0001;
	}
}
